package com.cn.ant.modules.volunteer.web;

import com.cn.ant.common.persistence.Page;
import com.cn.ant.common.utils.AjaxJson;
import com.cn.ant.common.utils.StringUtils;
import com.cn.ant.common.web.BaseController;
import com.cn.ant.modules.user.entity.UserMain;
import com.cn.ant.modules.user.service.UserMainService;
import com.cn.ant.modules.volunteer.entity.Volunteer;
import com.cn.ant.modules.volunteer.service.VolunteerService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 志愿组合Controller
 * @author 黄根华
 * @version 2015-10-11
 */
@Controller
@RequestMapping(value = "${adminPath}/volunteer/volunteer")
public class VolunteerController extends BaseController {

	@Autowired
	private VolunteerService volunteerService;
	@Autowired
	private UserMainService userMainService;
	
	@ModelAttribute
	public Volunteer get(@RequestParam(required=false) String id) {
		Volunteer entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = volunteerService.get(id);
		}
		if (entity == null){
			entity = new Volunteer();
		}
		return entity;
	}
	
	@RequiresPermissions("volunteer:volunteer:view")
	@RequestMapping(value = {"list", ""})
	public String list(Volunteer volunteer, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Volunteer> page = volunteerService.findPage(new Page<Volunteer>(request, response), volunteer); 
		model.addAttribute("page", page);
		return "modules/volunteer/volunteerList";
	}

	@RequiresPermissions("volunteer:volunteer:view")
	@RequestMapping(value = "form")
	public String form(Volunteer volunteer, Model model) {
		model.addAttribute("volunteer", volunteer);
		return "modules/volunteer/volunteerForm";
	}

	@RequiresPermissions("volunteer:volunteer:edit")
	@RequestMapping(value = "save")
	@ResponseBody
	public AjaxJson save(Volunteer volunteer, HttpServletRequest request,Model model) {
		AjaxJson jo = new AjaxJson();
		try {
			if (!beanValidator(model, volunteer)) {
                jo.setSuccess(false);
                jo.setMsg("数据校验失败,请重试!");
                return jo;
			} else {
			    volunteerService.save(volunteer);
			}
			msg = "保存志愿组合'" + volunteer.getName() + "'成功";
            jo.setMsg(msg);
            jo.setSuccess(true);
		} catch (Exception e) {
			e.printStackTrace();
            jo.setSuccess(false);
            jo.setMsg("操作失败,请联系管理员");
		}
		return jo;
	}
	
	@RequiresPermissions("volunteer:volunteer:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public AjaxJson delete(Volunteer volunteer, RedirectAttributes redirectAttributes) {
		AjaxJson jo = new AjaxJson();
        if (StringUtils.isNotBlank(volunteer.getId())) {
            volunteerService.delete(volunteer);
            jo.setMsg("删除志愿组合成功");
        } else {
            jo.setSuccess(false);
            jo.setMsg("参数错误,删除失败");
        }
        return jo;
	}

	/**
	 * 生成志愿组合
	 * @param request
	 * @param response
	 * @return
	 */
	public String createVolunteer(HttpServletRequest request, HttpServletResponse response) {
		String userId = request.getParameter("userId");
		int score = Integer.parseInt(request.getParameter("score"));
		UserMain userMain = userMainService.get(userId);

		return "";
	}

}